# agents/factor_agents/phainon_agent.py
from .base_factor_agent import BaseFactorAgent
from game_engine.game_state import FactorDevelopment, GameWorld
import numpy as np

class PhainonAgent(BaseFactorAgent):
    """白厄智能体 - 变身跑条特化 [火种：负世]"""
    
    def __init__(self, factor: FactorDevelopment, game_world: GameWorld):
        super().__init__(factor, game_world)
        self.personality_traits.update({
            "aggression": 0.5,      # 中等攻击性
            "caution": 0.5,         # 中等谨慎性
            "transformation_focus": 0.9,  # 变身专注
            "negative_world_affinity": 0.8  # 负世亲和
        })
        self.current_form = "base"  # base, negative, world
        self.transformation_gauge = 0
        self.last_transformation = 0
        self.form_durations = {"base": 0, "negative": 0, "world": 0}
    
    def make_decision(self, current_time: int) -> str:
        """白厄的决策逻辑"""
        situation = self.evaluate_situation()
        
        # 变身管理
        if self.transformation_gauge >= 100:
            if situation["need_negative_form"]:
                return "transform_negative"
            elif situation["need_world_form"]:
                return "transform_world"
            else:
                return "transform_base"
        
        # 当前形态下的行动
        if self.current_form == "negative":
            if situation["nearby_threats"]:
                target = situation["nearby_threats"][0]
                return f"negative_attack:{target.name}"
            else:
                return "accumulate_negativity"
        
        elif self.current_form == "world":
            if situation["allies_need_world"]:
                return "world_support"
            else:
                return "expand_world_influence"
        
        else:  # base form
            # 积累变身能量
            if situation["safe_to_charge"]:
                return "charge_transformation"
            elif situation["can_quick_charge"]:
                return "quick_charge"
            else:
                return "defensive_charge"
    
    def evaluate_situation(self) -> Dict:
        """评估当前局势"""
        nearby_factors = self.get_nearby_factors()
        
        threats = [f for f in nearby_factors if self._is_potential_threat(f)]
        allies = [f for f in nearby_factors if not self._is_potential_threat(f)]
        
        need_negative_form = (len(threats) >= 2 or 
                            any(t.attributes['attack'] > 40 for t in threats))
        
        need_world_form = (len(allies) >= 3 or 
                          any(a.attributes['health'] < 40 for a in allies))
        
        safe_to_charge = (len(threats) == 0 or 
                         all(self._calculate_distance(
                             self.factor.current_location, t.current_location) > 20 
                             for t in threats))
        
        can_quick_charge = (len(threats) == 1 and 
                           threats[0].attributes['attack'] < 30)
        
        allies_need_world = (len([a for a in allies if a.attributes['health'] < 60]) >= 2 or
                            len(allies) >= 4)
        
        return {
            "nearby_threats": threats,
            "need_negative_form": need_negative_form,
            "need_world_form": need_world_form,
            "safe_to_charge": safe_to_charge,
            "can_quick_charge": can_quick_charge,
            "allies_need_world": allies_need_world,
            "transformation_gauge": self.transformation_gauge,
            "current_form": self.current_form,
            "form_duration": self.form_durations.get(self.current_form, 0)
        }
    
    def transform(self, new_form: str):
        """执行变身"""
        old_form = self.current_form
        self.current_form = new_form
        self.transformation_gauge = 0
        self.last_transformation = self.game_world.current_time
        
        # 记录形态持续时间
        if old_form in self.form_durations:
            self.form_durations[old_form] = 0
        
        return f"变身为 {new_form} 形态"
    
    def charge_gauge(self, amount: int):
        """充能变身条"""
        self.transformation_gauge = min(100, self.transformation_gauge + amount)
        
        # 形态持续时间累积
        if self.current_form in self.form_durations:
            self.form_durations[self.current_form] += 1
    
    def get_form_bonuses(self) -> Dict[str, float]:
        """获取当前形态加成"""
        bonuses = {
            "base": {"attack": 1.0, "defense": 1.0, "mobility": 1.0},
            "negative": {"attack": 1.8, "defense": 0.7, "mobility": 1.2},
            "world": {"attack": 0.7, "defense": 1.5, "mobility": 0.9}
        }
        return bonuses.get(self.current_form, bonuses["base"])